package com.smart.config;

import com.smart.config.core.ConfigChangeListener;
import com.smart.config.core.ConfigManager;
import com.smart.config.core.ConfigManagerImpl;
import com.smart.config.core.DynamicValueProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

/**
 * @Author xiaoqiang
 * @Version SmartConfigration.java, v 0.1 2025年03月18日 15:16 xiaoqiang
 * @Description: TODO
 */
@Configuration
@EnableConfigurationProperties(ConfigProperties.class)
@ConditionalOnProperty(prefix = "smart.config", name = "enabled", matchIfMissing = false)
@Component
public class SmartConfigration {

    @Autowired
    private ConfigProperties configProperties;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Bean
    public DynamicValueProcessor dynamicValueProcessor(ConfigManager configManager) {
        return new DynamicValueProcessor(configManager, configProperties);
    }

    @Bean
    public ConfigManager configManager() {
        return new ConfigManagerImpl(redisTemplate, configProperties);
    }

    @Bean(initMethod = "init")
    public ConfigChangeListener configChangeListener(ConfigManager configManager) {

        return new ConfigChangeListener(redisTemplate, configProperties, configManager);
    }


}
